package 力扣_高阶数据结构.优先级队列;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class 数组中的第K个最大元素_215 {
    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2){
                return o1 - o2;
            }
        });
        for(int i = 0;i < nums.length;i++){
            if(queue.size() < k){
                queue.offer(nums[i]);
            }else{
                int peek = queue.peek();
                if(nums[i] < peek){
                    continue;
                }else{
                    queue.poll();
                    queue.offer(nums[i]);
                }
            }
        }
        return queue.poll();
    }
}
